<?php

namespace App\Admin\Actions\ShopProduct;

use App\Library\Logic\OperateLogLogic;
use App\Library\Logic\ShopProductLogic;
use App\Library\Models\AdminShopProductModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Exception;

class ShopProductCopyAction extends RowAction
{
    public $name = '复制';

    /**
     * handle
     * @param AdminShopProductModel $model
     * @return Response
     * @throws Exception
     */
    public function handle(AdminShopProductModel $model): Response
    {
        $userId = Admin::user()->user_id;
        $cache = cache('user:' . $userId);
        $mainId = Admin::user()->user_main_id === 0 ? $userId : Admin::user()->user_main_id;
        ShopProductLogic::copy(ShopProductLogic::detail($model->product_id), $mainId);
        sleep(2);
        OperateLogLogic::add(Admin::user()->user_id, 'admin.product.copy', ['productId' => $model->product_id]);
        return $this->response()->success('操作成功')->refresh();
    }

    /**
     * dialog
     * @return void
     */
    public function dialog(): void
    {
        $this->confirm('确定复制吗?');
    }
}
